<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang='en'>
  <head>
    <title>KRPC::Client</title>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
    <link href='../../css/style.css' media='screen' rel='stylesheet' type='text/css'>
    <script type='text/javascript'>
      //<![CDATA[
        function popupCode(url) {
          window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
        }
        
        function toggleCode(id) {
          var code = document.getElementById(id)
        
          code.style.display = code.style.display != 'block' ? 'block' : 'none'
          return true
        }
        
        // Make codeblocks hidden by default
        document.writeln('<' + 'style type="text/css">.method .source pre { display: none }<\/style>')
      //]]>
    </script>
  </head>
  <body class='page'>
    <div class='class' id='wrapper'>
      <div class='header'>
        <h1 class='name'>
          <span class='type'>class</span>
          KRPC::Client
        </h1>
        <ol class='paths'>
          <li>
            <a target="docwin" href="../../files/lib/krpc/client_rb.html">lib/krpc/client.rb</a>
          </li>
        </ol>
        <div class='parent'>
          Superclass:
          <strong><a target="docwin" href="../Object.html">Object</a></strong>
        </div>
      </div>
      <div id='content'>
        <div id='text'>
          <div id='description'>
            
            <p>A kRPC client, through which all Remote Procedure Calls are made. To make
            RPC calls client must first connect to server. This can be achieved by
            calling <a href="Client.html#method-i-connect">#connect</a> or <a
            href="Client.html#method-i-connect-21">#connect!</a> methods. <a
            href="Client.html">Client</a> object can connect and disconnect from the
            server many times during it&#39;s lifetime. RPCs can be made by calling <a
            href="Client.html#method-i-execute_rpc">#execute_rpc</a> method. After
            generating services API (with <a
            href="Client.html#method-i-generate_services_api-21">#generate_services_api!</a>
            call), RPCs can be also made using
            <code>client.service_name.procedure_name(parameter, ...)</code></p>
            
            <h3 id="class-KRPC::Client-label-Example-3A">Example:<span><a href="#class-KRPC::Client-label-Example-3A">&para;</a> <a href="#top">&uarr;</a></span></h3>
            
            <pre class="ruby"><span class="ruby-identifier">client</span> = <span class="ruby-constant">KRPC</span><span class="ruby-operator">::</span><span class="ruby-constant">Client</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">name</span><span class="ruby-operator">:</span> <span class="ruby-string">&quot;my client&quot;</span>).<span class="ruby-identifier">connect!</span>&#x000A;<span class="ruby-identifier">ctrl</span> = <span class="ruby-identifier">client</span>.<span class="ruby-identifier">space_center</span>.<span class="ruby-identifier">active_vessel</span>.<span class="ruby-identifier">control</span>&#x000A;<span class="ruby-identifier">ctrl</span>.<span class="ruby-identifier">activate_next_stage</span>&#x000A;<span class="ruby-identifier">ctrl</span>.<span class="ruby-identifier">throttle</span> = <span class="ruby-value">1</span> <span class="ruby-comment"># Full ahead!</span>&#x000A;<span class="ruby-identifier">client</span>.<span class="ruby-identifier">close</span> <span class="ruby-comment"># Gracefully disconnect - and allow the spacecraft to crash ;)</span>&#x000A;<span class="ruby-identifier">client</span>.<span class="ruby-identifier">connect</span> <span class="ruby-keyword">do</span> <span class="ruby-comment"># Connect to server again</span>&#x000A;  <span class="ruby-identifier">client</span>.<span class="ruby-identifier">space_center</span>.<span class="ruby-identifier">active_vessel</span>.<span class="ruby-identifier">control</span>.<span class="ruby-identifier">throttle</span> = <span class="ruby-value">0</span> <span class="ruby-comment"># Save the spacecraft from imminent destruction ;)</span>&#x000A;<span class="ruby-keyword">end</span> <span class="ruby-comment"># Gracefully disconnect</span></pre>
          </div>
          <div id='method-list'>
            <h2>Methods</h2>
            <h3>Public Class</h3>
            <ol>
              <li><a target="docwin" href="#method-c-new">new</a></li>
            </ol>
            <h3>Public Instance</h3>
            <ol>
              <li><a target="docwin" href="#method-i-build_request">build_request</a></li>
              <li><a target="docwin" href="#method-i-close">close</a></li>
              <li><a target="docwin" href="#method-i-connect">connect</a></li>
              <li><a target="docwin" href="#method-i-connect-21">connect!</a></li>
              <li><a target="docwin" href="#method-i-connected-3F">connected?</a></li>
              <li><a target="docwin" href="#method-i-execute_rpc">execute_rpc</a></li>
              <li><a target="docwin" href="#method-i-generate_services_api-21">generate_services_api!</a></li>
              <li><a target="docwin" href="#attribute-i-krpc">krpc</a></li>
              <li><a target="docwin" href="#attribute-i-name">name</a></li>
              <li><a target="docwin" href="#attribute-i-rpc_connection">rpc_connection</a></li>
              <li><a target="docwin" href="#method-i-services_api_generated-3F">services_api_generated?</a></li>
              <li><a target="docwin" href="#attribute-i-stream_connection">stream_connection</a></li>
              <li><a target="docwin" href="#attribute-i-streams_manager">streams_manager</a></li>
            </ol>
            <h3>Protected Instance</h3>
            <ol>
              <li><a target="docwin" href="#method-i-call_block_and_close">call_block_and_close</a></li>
              <li><a target="docwin" href="#method-i-construct_arguments">construct_arguments</a></li>
              <li><a target="docwin" href="#method-i-receive_response">receive_response</a></li>
              <li><a target="docwin" href="#method-i-send_request">send_request</a></li>
            </ol>
          </div>
          <div id='context'>
            <div id='includes'>
              <h2>Included modules</h2>
              <ol>
                <li><a target="docwin" href="Doc/SuffixMethods.html">Doc::SuffixMethods</a></li>
              </ol>
            </div>
          </div>
          <div id='section'>
            <div id='constants-list'>
              <h2>Constants</h2>
              <div class='name-list'>
                <table summary='Constants'>
                  <tr class='top-aligned-row context-row'>
                    <td class='context-item-name'>DEFAULT_NAME</td>
                    <td>=</td>
                    <td class='context-item-value'>""</td>
                    <td>&nbsp;</td>
                    <td class='context-item-desc'></td>
                  </tr>
                </table>
              </div>
            </div>
            <div id='attribute-list'>
              <h2 class='section-bar'>Attributes</h2>
              <div class='name-list'>
                <table>
                  <tr class='top-aligned-row context-row'>
                    <td class='context-item-name'>
                      <a name='attribute-i-krpc'>krpc</a>
                    </td>
                    <td class='context-item-value'>[R]</td>
                    <td class='context-item-desc'></td>
                  </tr>
                  <tr class='top-aligned-row context-row'>
                    <td class='context-item-name'>
                      <a name='attribute-i-name'>name</a>
                    </td>
                    <td class='context-item-value'>[R]</td>
                    <td class='context-item-desc'></td>
                  </tr>
                  <tr class='top-aligned-row context-row'>
                    <td class='context-item-name'>
                      <a name='attribute-i-rpc_connection'>rpc_connection</a>
                    </td>
                    <td class='context-item-value'>[R]</td>
                    <td class='context-item-desc'></td>
                  </tr>
                  <tr class='top-aligned-row context-row'>
                    <td class='context-item-name'>
                      <a name='attribute-i-stream_connection'>stream_connection</a>
                    </td>
                    <td class='context-item-value'>[R]</td>
                    <td class='context-item-desc'></td>
                  </tr>
                  <tr class='top-aligned-row context-row'>
                    <td class='context-item-name'>
                      <a name='attribute-i-streams_manager'>streams_manager</a>
                    </td>
                    <td class='context-item-value'>[R]</td>
                    <td class='context-item-desc'></td>
                  </tr>
                </table>
              </div>
            </div>
            <div id='methods'>
              <h2>Public Class methods</h2>
              <div class='method public-class' id='method-method-c-new'>
                <a name='method-c-new'></a>
                <div class='synopsis'>
                  <span class='name'>new</span>
                  <span class='arguments'>(name: DEFAULT_NAME, host: Connection::DEFAULT_SERVER_HOST, rpc_port: Connection::DEFAULT_SERVER_RPC_PORT, stream_port: Connection::DEFAULT_SERVER_STREAM_PORT)</span>
                </div>
                <div class='description'>
                  
                  <p>Create new <a href="Client.html">Client</a> object, optionally specifying
                  IP address and port numbers on witch kRPC server is listening and the name
                  for this client (up to 32 bytes of UTF-8 encoded text).</p>
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-c-new-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-c-new-source'><span class="ruby-comment"># File lib/krpc/client.rb, line 39</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">name</span><span class="ruby-operator">:</span> <span class="ruby-constant">DEFAULT_NAME</span>, <span class="ruby-identifier">host</span><span class="ruby-operator">:</span> <span class="ruby-constant">Connection</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_SERVER_HOST</span>, <span class="ruby-identifier">rpc_port</span><span class="ruby-operator">:</span> <span class="ruby-constant">Connection</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_SERVER_RPC_PORT</span>, <span class="ruby-identifier">stream_port</span><span class="ruby-operator">:</span> <span class="ruby-constant">Connection</span><span class="ruby-operator">::</span><span class="ruby-constant">DEFAULT_SERVER_STREAM_PORT</span>)&#x000A;  <span class="ruby-ivar">@name</span> = <span class="ruby-identifier">name</span>&#x000A;  <span class="ruby-ivar">@rpc_connection</span> = <span class="ruby-constant">RPCConnection</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">host</span>, <span class="ruby-identifier">rpc_port</span>)&#x000A;  <span class="ruby-ivar">@stream_connection</span> = <span class="ruby-constant">StreamConnection</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">rpc_connection</span>, <span class="ruby-identifier">host</span>, <span class="ruby-identifier">stream_port</span>)&#x000A;  <span class="ruby-ivar">@streams_manager</span> = <span class="ruby-constant">Streaming</span><span class="ruby-operator">::</span><span class="ruby-constant">StreamsManager</span>.<span class="ruby-identifier">new</span>(<span class="ruby-keyword">self</span>)&#x000A;  <span class="ruby-ivar">@services</span> = {}&#x000A;  <span class="ruby-ivar">@krpc</span> = <span class="ruby-constant">Services</span><span class="ruby-operator">::</span><span class="ruby-constant">KRPC</span>.<span class="ruby-identifier">new</span>(<span class="ruby-keyword">self</span>)&#x000A;  <span class="ruby-constant">Doc</span>.<span class="ruby-identifier">add_docstring_info</span>(<span class="ruby-keyword">false</span>, <span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>, <span class="ruby-string">&quot;krpc&quot;</span>, <span class="ruby-identifier">return_type</span><span class="ruby-operator">:</span> <span class="ruby-ivar">@krpc</span>.<span class="ruby-identifier">class</span>, <span class="ruby-identifier">xmldoc</span><span class="ruby-operator">:</span> <span class="ruby-string">&quot;&lt;doc&gt;&lt;summary&gt;Core kRPC service, e.g. for querying for the available services. Most of this functionality is used internally by the Ruby client and therefore does not need to be used directly from application code.&lt;/summary&gt;&lt;/doc&gt;&quot;</span>)&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <h2>Public Instance methods</h2>
              <div class='method public-instance' id='method-method-i-build_request'>
                <a name='method-i-build_request'></a>
                <div class='synopsis'>
                  <span class='name'>build_request</span>
                  <span class='arguments'>(service, procedure, args=[], kwargs={}, param_names=[], param_types=[], param_default=[])</span>
                </div>
                <div class='description'>
                  
                  <p>Build a <a href="PB/Request.html">PB::Request</a> object.</p>
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-build_request-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-build_request-source'><span class="ruby-comment"># File lib/krpc/client.rb, line 148</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">build_request</span>(<span class="ruby-identifier">service</span>, <span class="ruby-identifier">procedure</span>, <span class="ruby-identifier">args</span>=[], <span class="ruby-identifier">kwargs</span>={}, <span class="ruby-identifier">param_names</span>=[], <span class="ruby-identifier">param_types</span>=[], <span class="ruby-identifier">param_default</span>=[])&#x000A;  <span class="ruby-keyword">begin</span>&#x000A;    <span class="ruby-identifier">raise</span>(<span class="ruby-constant">ArgumentError</span>, <span class="ruby-node">&quot;param_names and param_types should be equal length\n\tparam_names = #{param_names}\n\tparam_types = #{param_types}&quot;</span>) <span class="ruby-keyword">unless</span> <span class="ruby-identifier">param_names</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">param_types</span>.<span class="ruby-identifier">length</span>&#x000A;    <span class="ruby-identifier">raise</span>(<span class="ruby-constant">ArgumentError</span>, <span class="ruby-node">&quot;param_names and param_default should be equal length\n\tparam_names = #{param_names}\n\tparam_default = #{param_default}&quot;</span>) <span class="ruby-keyword">unless</span> <span class="ruby-identifier">param_names</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">param_default</span>.<span class="ruby-identifier">length</span>&#x000A;    <span class="ruby-identifier">required_params_count</span> = <span class="ruby-identifier">param_default</span>.<span class="ruby-identifier">take_while</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">pd</span><span class="ruby-operator">|</span> <span class="ruby-identifier">pd</span> <span class="ruby-operator">==</span> <span class="ruby-value">:no_default_value</span>}.<span class="ruby-identifier">count</span>&#x000A;    <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentsNumberErrorSig</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">args</span>.<span class="ruby-identifier">count</span>, <span class="ruby-identifier">required_params_count</span><span class="ruby-operator">..</span><span class="ruby-identifier">param_names</span>.<span class="ruby-identifier">count</span>) <span class="ruby-keyword">unless</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">count</span> <span class="ruby-operator">&lt;=</span> <span class="ruby-identifier">param_names</span>.<span class="ruby-identifier">count</span>&#x000A;    <span class="ruby-identifier">req_args</span> = <span class="ruby-identifier">construct_arguments</span>(<span class="ruby-identifier">args</span>, <span class="ruby-identifier">kwargs</span>, <span class="ruby-identifier">param_names</span>, <span class="ruby-identifier">param_types</span>, <span class="ruby-identifier">param_default</span>, <span class="ruby-identifier">required_params_count</span>)&#x000A;  <span class="ruby-keyword">rescue</span> <span class="ruby-constant">ArgumentErrorSig</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">err</span>&#x000A;    <span class="ruby-identifier">raise</span> <span class="ruby-identifier">err</span>.<span class="ruby-identifier">with_signature</span>(<span class="ruby-constant">Doc</span>.<span class="ruby-identifier">docstring_for_procedure</span>(<span class="ruby-identifier">service</span>, <span class="ruby-identifier">procedure</span>, <span class="ruby-keyword">false</span>))&#x000A;  <span class="ruby-keyword">end</span>&#x000A;  <span class="ruby-constant">PB</span><span class="ruby-operator">::</span><span class="ruby-constant">Request</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">service</span><span class="ruby-operator">:</span> <span class="ruby-identifier">service</span>, <span class="ruby-identifier">procedure</span><span class="ruby-operator">:</span> <span class="ruby-identifier">procedure</span>, <span class="ruby-identifier">arguments</span><span class="ruby-operator">:</span> <span class="ruby-identifier">req_args</span>)&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-instance' id='method-method-i-close'>
                <a name='method-i-close'></a>
                <div class='synopsis'>
                  <span class='name'>close</span>
                  <span class='arguments'>()</span>
                </div>
                <div class='description'>
                  
                  <p>Close connection to kRPC server. Returns <code>true</code> if the
                  connection has closed or <code>false</code> if the client had been already
                  disconnected.</p>
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-close-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-close-source'><span class="ruby-comment"># File lib/krpc/client.rb, line 73</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">close</span>&#x000A;  <span class="ruby-identifier">streams_manager</span>.<span class="ruby-identifier">remove_all_streams</span>&#x000A;  <span class="ruby-identifier">streams_manager</span>.<span class="ruby-identifier">stop_streaming_thread</span>&#x000A;  <span class="ruby-identifier">stream_connection</span>.<span class="ruby-identifier">close</span>&#x000A;  <span class="ruby-identifier">rpc_connection</span>.<span class="ruby-identifier">close</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-instance' id='method-method-i-connect'>
                <a name='method-i-connect'></a>
                <div class='synopsis'>
                  <span class='name'>connect</span>
                  <span class='arguments'>(&block)</span>
                </div>
                <div class='description'>
                  
                  <p>Connect to a kRPC server on the IP address and port numbers specified
                  during this client object creation and return <code>self</code>. Calling
                  this method while the client is already connected will raise an exception.
                  If the block is given, then it&#39;s called passing <code>self</code> and
                  the connection to kRPC server is closed at the end of the block.</p>
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-connect-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-connect-source'><span class="ruby-comment"># File lib/krpc/client.rb, line 53</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">connect</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)&#x000A;  <span class="ruby-identifier">rpc_connection</span>.<span class="ruby-identifier">connect</span>&#x000A;  <span class="ruby-identifier">stream_connection</span>.<span class="ruby-identifier">connect</span>&#x000A;  <span class="ruby-identifier">streams_manager</span>.<span class="ruby-identifier">start_streaming_thread</span>&#x000A;  <span class="ruby-identifier">call_block_and_close</span>(<span class="ruby-identifier">block</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span>&#x000A;  <span class="ruby-keyword">self</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-instance' id='method-method-i-connect-21'>
                <a name='method-i-connect-21'></a>
                <div class='synopsis'>
                  <span class='name'>connect!</span>
                  <span class='arguments'>(&block)</span>
                </div>
                <div class='description'>
                  
                  <p>Connect to a kRPC server, generate services API and return
                  <code>self</code>. Shorthand for calling <a
                  href="Client.html#method-i-connect">#connect</a> and <a
                  href="Client.html#method-i-generate_services_api-21">#generate_services_api!</a>
                  subsequently. If the block is given, then it&#39;s called passing
                  <code>self</code> and the connection to kRPC server is closed at the end of
                  the block.</p>
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-connect-21-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-connect-21-source'><span class="ruby-comment"># File lib/krpc/client.rb, line 64</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">connect!</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)&#x000A;  <span class="ruby-identifier">connect</span>&#x000A;  <span class="ruby-identifier">generate_services_api!</span>&#x000A;  <span class="ruby-identifier">call_block_and_close</span>(<span class="ruby-identifier">block</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span>&#x000A;  <span class="ruby-keyword">self</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-instance' id='method-method-i-connected-3F'>
                <a name='method-i-connected-3F'></a>
                <div class='synopsis'>
                  <span class='name'>connected?</span>
                  <span class='arguments'>()</span>
                </div>
                <div class='description'>
                  
                  <p>Returns <code>true</code> if the client is connected to a server,
                  <code>false</code> otherwise.</p>
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-connected-3F-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-connected-3F-source'><span class="ruby-comment"># File lib/krpc/client.rb, line 81</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">connected?</span>&#x000A;  <span class="ruby-identifier">rpc_connection</span>.<span class="ruby-identifier">connected?</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-instance' id='method-method-i-execute_rpc'>
                <a name='method-i-execute_rpc'></a>
                <div class='synopsis'>
                  <span class='name'>execute_rpc</span>
                  <span class='arguments'>(service, procedure, args=[], kwargs={}, param_names=[], param_types=[], param_default=[], return_type: nil)</span>
                </div>
                <div class='description'>
                  
                  <p>Execute an RPC.</p>
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-execute_rpc-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-execute_rpc-source'><span class="ruby-comment"># File lib/krpc/client.rb, line 133</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">execute_rpc</span>(<span class="ruby-identifier">service</span>, <span class="ruby-identifier">procedure</span>, <span class="ruby-identifier">args</span>=[], <span class="ruby-identifier">kwargs</span>={}, <span class="ruby-identifier">param_names</span>=[], <span class="ruby-identifier">param_types</span>=[], <span class="ruby-identifier">param_default</span>=[], <span class="ruby-identifier">return_type</span><span class="ruby-operator">:</span> <span class="ruby-keyword">nil</span>)&#x000A;  <span class="ruby-identifier">send_request</span>(<span class="ruby-identifier">service</span>, <span class="ruby-identifier">procedure</span>, <span class="ruby-identifier">args</span>, <span class="ruby-identifier">kwargs</span>, <span class="ruby-identifier">param_names</span>, <span class="ruby-identifier">param_types</span>, <span class="ruby-identifier">param_default</span>)&#x000A;  <span class="ruby-identifier">resp</span> = <span class="ruby-identifier">receive_response</span>&#x000A;  <span class="ruby-identifier">raise</span>(<span class="ruby-constant">RPCError</span>, <span class="ruby-identifier">resp</span>.<span class="ruby-identifier">error</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">resp</span>.<span class="ruby-identifier">has_field?</span> <span class="ruby-string">&quot;error&quot;</span>&#x000A;  <span class="ruby-keyword">unless</span> <span class="ruby-identifier">return_type</span>.<span class="ruby-identifier">nil?</span>&#x000A;    <span class="ruby-constant">Decoder</span>.<span class="ruby-identifier">decode</span>(<span class="ruby-identifier">resp</span>.<span class="ruby-identifier">return_value</span>, <span class="ruby-identifier">return_type</span>, <span class="ruby-keyword">self</span>)&#x000A;  <span class="ruby-keyword">else</span>&#x000A;    <span class="ruby-keyword">nil</span>&#x000A;  <span class="ruby-keyword">end</span>&#x000A;<span class="ruby-keyword">rescue</span> <span class="ruby-constant">IOError</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span>&#x000A;  <span class="ruby-identifier">raise</span>(<span class="ruby-constant">Exception</span>, <span class="ruby-node">&quot;RPC call attempt while not connected to server -- call Client#connect first&quot;</span>) <span class="ruby-keyword">if</span> <span class="ruby-keyword">not</span> <span class="ruby-identifier">connected?</span>&#x000A;  <span class="ruby-identifier">raise</span> <span class="ruby-identifier">e</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-instance' id='method-method-i-generate_services_api-21'>
                <a name='method-i-generate_services_api-21'></a>
                <div class='synopsis'>
                  <span class='name'>generate_services_api!</span>
                  <span class='arguments'>()</span>
                </div>
                <div class='description'>
                  
                  <p>Interrogates the server to find out what functionality it provides and
                  dynamically creates all of the classes and methods that form services API.
                  For each service that server provides:</p>
                  <ol><li>
                  <p>Class <code>KRPC::Services::{service name here}</code>, and module
                  <code>KRPC::Gen::{service name here}</code>  are created.</p>
                  </li><li>
                  <p><code>KRPC::Gen::{service name here}</code> module is filled with
                  dynamically created classes.</p>
                  </li><li>
                  <p>Those classes in turn are filled with dynamically created methods, that
                  form API for  this service.</p>
                  </li><li>
                  <p>Instance method <code>{service name here}</code> is created in this client
                  object that returns  <code>KRPC::Services::{service name here}</code>
                  object. This object is entry point for accessing  functionality provided by
                  <code>{service name here}</code> service.</p>
                  </li></ol>
                  
                  <p>Returns <code>self</code>. Invoking this method the second and subsequent
                  times doesn&#39;t regenerate API. To regenerate API create new <a
                  href="Client.html">Client</a> object and call <a
                  href="Client.html#method-i-generate_services_api-21">generate_services_api!</a>
                  on it.</p>
                  
                  <h3 id="method-i-generate_services_api-21-label-Example">Example<span><a href="#method-i-generate_services_api-21-label-Example">&para;</a> <a href="#top">&uarr;</a></span></h3>
                  
                  <pre class="ruby">  <span class="ruby-identifier">client</span> = <span class="ruby-constant">KRPC</span><span class="ruby-operator">::</span><span class="ruby-constant">Client</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">name</span><span class="ruby-operator">:</span> <span class="ruby-string">&quot;my client&quot;</span>).<span class="ruby-identifier">connect</span> <span class="ruby-comment"># Notice that it is &#39;Client#connect&#39; being called, not &#39;Client#connect!&#39;</span>&#x000A;  <span class="ruby-identifier">sc</span> = <span class="ruby-identifier">client</span>.<span class="ruby-identifier">space_center</span> <span class="ruby-comment"># =&gt; Exception (undefined method &quot;space_center&quot;)</span>&#x000A;  <span class="ruby-identifier">client</span>.<span class="ruby-identifier">generate_services_api!</span>&#x000A;  <span class="ruby-identifier">sc</span> = <span class="ruby-identifier">client</span>.<span class="ruby-identifier">space_center</span> <span class="ruby-comment"># =&gt; KRPC::Services::SpaceCenter object</span>&#x000A;  <span class="ruby-identifier">v</span>  = <span class="ruby-identifier">sc</span>.<span class="ruby-identifier">active_vessel</span>    <span class="ruby-comment"># =&gt; KRPC::Gen::SpaceCenter::Vessel object</span>&#x000A;  <span class="ruby-identifier">v</span>.<span class="ruby-identifier">mass</span>                   <span class="ruby-comment"># =&gt; {some number here}</span>&#x000A;  <span class="ruby-identifier">client</span>.<span class="ruby-identifier">close</span></pre>
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-generate_services_api-21-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-generate_services_api-21-source'><span class="ruby-comment"># File lib/krpc/client.rb, line 108</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">generate_services_api!</span>&#x000A;  <span class="ruby-keyword">return</span> <span class="ruby-keyword">self</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">services_api_generated?</span>&#x000A;  <span class="ruby-identifier">raise</span>(<span class="ruby-constant">Exception</span>, <span class="ruby-node">&quot;Can&#39;t generate services API while not connected to server -- call Client#connect! to connect to server and generate services API in one call&quot;</span>) <span class="ruby-keyword">if</span> <span class="ruby-keyword">not</span> <span class="ruby-identifier">connected?</span>&#x000A;  &#x000A;  <span class="ruby-identifier">resp</span> = <span class="ruby-identifier">krpc</span>.<span class="ruby-identifier">get_services</span>&#x000A;  <span class="ruby-identifier">resp</span>.<span class="ruby-identifier">services</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">service_msg</span><span class="ruby-operator">|</span>&#x000A;    <span class="ruby-keyword">next</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">service_msg</span>.<span class="ruby-identifier">name</span> <span class="ruby-operator">==</span> <span class="ruby-string">&quot;KRPC&quot;</span>&#x000A;    <span class="ruby-identifier">service_class</span> = <span class="ruby-constant">Services</span>.<span class="ruby-identifier">create_service</span>(<span class="ruby-identifier">service_msg</span>)&#x000A;    <span class="ruby-identifier">method_name</span> = <span class="ruby-identifier">service_class</span>.<span class="ruby-identifier">class_name</span>.<span class="ruby-identifier">underscore</span>&#x000A;    <span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">instance_eval</span> <span class="ruby-keyword">do</span>&#x000A;      <span class="ruby-identifier">define_method</span> <span class="ruby-identifier">method_name</span> <span class="ruby-keyword">do</span>&#x000A;        <span class="ruby-ivar">@services</span>[<span class="ruby-identifier">service_class</span>.<span class="ruby-identifier">class_name</span>] <span class="ruby-operator">||=</span> <span class="ruby-identifier">service_class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-keyword">self</span>)&#x000A;      <span class="ruby-keyword">end</span>&#x000A;    <span class="ruby-keyword">end</span>&#x000A;    <span class="ruby-constant">Doc</span>.<span class="ruby-identifier">add_docstring_info</span>(<span class="ruby-keyword">false</span>, <span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>, <span class="ruby-identifier">method_name</span>, <span class="ruby-identifier">return_type</span><span class="ruby-operator">:</span> <span class="ruby-identifier">service_class</span>, <span class="ruby-identifier">xmldoc</span><span class="ruby-operator">:</span> <span class="ruby-identifier">service_msg</span>.<span class="ruby-identifier">documentation</span>)&#x000A;  <span class="ruby-keyword">end</span>&#x000A;  <span class="ruby-keyword">self</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-instance' id='method-method-i-services_api_generated-3F'>
                <a name='method-i-services_api_generated-3F'></a>
                <div class='synopsis'>
                  <span class='name'>services_api_generated?</span>
                  <span class='arguments'>()</span>
                </div>
                <div class='description'>
                  
                  <p>Returns <code>true</code> if services API has been already generated,
                  <code>false</code> otherwise.</p>
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-services_api_generated-3F-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-services_api_generated-3F-source'><span class="ruby-comment"># File lib/krpc/client.rb, line 128</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">services_api_generated?</span>&#x000A;  <span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:space_center</span> <span class="ruby-keyword">or</span> <span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:test_service</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <h2>Protected Instance methods</h2>
              <div class='method protected-instance' id='method-method-i-call_block_and_close'>
                <a name='method-i-call_block_and_close'></a>
                <div class='synopsis'>
                  <span class='name'>call_block_and_close</span>
                  <span class='arguments'>(block)</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-call_block_and_close-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-call_block_and_close-source'><span class="ruby-comment"># File lib/krpc/client.rb, line 209</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">call_block_and_close</span>(<span class="ruby-identifier">block</span>)&#x000A;  <span class="ruby-keyword">begin</span> <span class="ruby-identifier">block</span>.<span class="ruby-identifier">call</span>(<span class="ruby-keyword">self</span>) <span class="ruby-keyword">ensure</span> <span class="ruby-identifier">close</span> <span class="ruby-keyword">end</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method protected-instance' id='method-method-i-construct_arguments'>
                <a name='method-i-construct_arguments'></a>
                <div class='synopsis'>
                  <span class='name'>construct_arguments</span>
                  <span class='arguments'>(args, kwargs, param_names, param_types, param_default, required_params_count)</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-construct_arguments-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-construct_arguments-source'><span class="ruby-comment"># File lib/krpc/client.rb, line 163</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">construct_arguments</span>(<span class="ruby-identifier">args</span>, <span class="ruby-identifier">kwargs</span>, <span class="ruby-identifier">param_names</span>, <span class="ruby-identifier">param_types</span>, <span class="ruby-identifier">param_default</span>, <span class="ruby-identifier">required_params_count</span>)&#x000A;  <span class="ruby-identifier">param_names_symbols</span> = <span class="ruby-identifier">param_names</span>.<span class="ruby-identifier">map</span>(<span class="ruby-operator">&amp;</span><span class="ruby-value">:to_sym</span>)&#x000A;  <span class="ruby-identifier">kwargs_remaining</span> = <span class="ruby-identifier">kwargs</span>.<span class="ruby-identifier">count</span>&#x000A;  &#x000A;  <span class="ruby-identifier">req_args</span> = <span class="ruby-identifier">param_names_symbols</span>.<span class="ruby-identifier">map</span>.<span class="ruby-identifier">with_index</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">name</span>, <span class="ruby-identifier">i</span><span class="ruby-operator">|</span>&#x000A;    <span class="ruby-identifier">is_kwarg</span> = <span class="ruby-identifier">kwargs</span>.<span class="ruby-identifier">has_key?</span> <span class="ruby-identifier">name</span>&#x000A;    <span class="ruby-identifier">kwargs_remaining</span> <span class="ruby-operator">-=</span> <span class="ruby-value">1</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">is_kwarg</span>&#x000A;    <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentErrorSig</span>.<span class="ruby-identifier">new</span>(<span class="ruby-node">&quot;there are both positional and keyword arguments for parameter \&quot;#{name}\&quot;&quot;</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">is_kwarg</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">i</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">count</span>&#x000A;    <span class="ruby-identifier">is_parameter_optional</span> = <span class="ruby-identifier">i</span> <span class="ruby-operator">&gt;=</span> <span class="ruby-identifier">required_params_count</span>&#x000A;    <span class="ruby-identifier">is_parameter_has_default_value</span> = <span class="ruby-operator">!</span><span class="ruby-identifier">is_kwarg</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">i</span> <span class="ruby-operator">&gt;=</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">count</span> <span class="ruby-operator">||</span>&#x000A;                                     <span class="ruby-operator">!</span><span class="ruby-identifier">is_kwarg</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">args</span>[<span class="ruby-identifier">i</span>] <span class="ruby-operator">==</span> <span class="ruby-identifier">param_default</span>[<span class="ruby-identifier">i</span>] <span class="ruby-operator">||</span>&#x000A;                                     <span class="ruby-identifier">is_kwarg</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">kwargs</span>[<span class="ruby-identifier">name</span>] <span class="ruby-operator">==</span> <span class="ruby-identifier">param_default</span>[<span class="ruby-identifier">i</span>]&#x000A;    <span class="ruby-keyword">unless</span> <span class="ruby-identifier">is_parameter_optional</span> <span class="ruby-keyword">and</span> <span class="ruby-identifier">is_parameter_has_default_value</span>&#x000A;      <span class="ruby-identifier">arg</span> = <span class="ruby-keyword">if</span> <span class="ruby-identifier">is_kwarg</span> <span class="ruby-keyword">then</span>&#x000A;              <span class="ruby-identifier">kwargs</span>[<span class="ruby-identifier">name</span>]&#x000A;            <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">i</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">count</span> <span class="ruby-keyword">then</span>&#x000A;              <span class="ruby-identifier">args</span>[<span class="ruby-identifier">i</span>]&#x000A;            <span class="ruby-keyword">else</span>&#x000A;              <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentErrorSig</span>.<span class="ruby-identifier">new</span>(<span class="ruby-node">&quot;missing argument for parameter \&quot;#{name}\&quot;&quot;</span>)&#x000A;            <span class="ruby-keyword">end</span>&#x000A;      <span class="ruby-keyword">begin</span>&#x000A;        <span class="ruby-identifier">arg</span> = <span class="ruby-constant">TypeStore</span>.<span class="ruby-identifier">coerce_to</span>(<span class="ruby-identifier">arg</span>, <span class="ruby-identifier">param_types</span>[<span class="ruby-identifier">i</span>])&#x000A;      <span class="ruby-keyword">rescue</span> <span class="ruby-constant">ValueError</span>&#x000A;        <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentErrorSig</span>.<span class="ruby-identifier">new</span>(<span class="ruby-node">&quot;argument for parameter \&quot;#{name}\&quot; must be a #{param_types[i].ruby_type} -- got #{args[i].inspect} of type #{args[i].class}&quot;</span>)&#x000A;      <span class="ruby-keyword">end</span>&#x000A;      <span class="ruby-identifier">v</span> = <span class="ruby-constant">Encoder</span>.<span class="ruby-identifier">encode</span>(<span class="ruby-identifier">arg</span>, <span class="ruby-identifier">param_types</span>[<span class="ruby-identifier">i</span>])&#x000A;      <span class="ruby-constant">PB</span><span class="ruby-operator">::</span><span class="ruby-constant">Argument</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">position</span><span class="ruby-operator">:</span> <span class="ruby-identifier">i</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">:</span> <span class="ruby-identifier">v</span>)&#x000A;    <span class="ruby-keyword">end</span>&#x000A;  <span class="ruby-keyword">end</span>.<span class="ruby-identifier">compact</span>&#x000A;  &#x000A;  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentErrorSig</span>.<span class="ruby-identifier">new</span>(<span class="ruby-node">&quot;keyword arguments for non existing parameters: #{(kwargs.keys - param_names_symbols).join(&quot;, &quot;)}&quot;</span>) <span class="ruby-keyword">unless</span> <span class="ruby-identifier">kwargs_remaining</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>&#x000A;  <span class="ruby-identifier">req_args</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method protected-instance' id='method-method-i-receive_response'>
                <a name='method-i-receive_response'></a>
                <div class='synopsis'>
                  <span class='name'>receive_response</span>
                  <span class='arguments'>()</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-receive_response-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-receive_response-source'><span class="ruby-comment"># File lib/krpc/client.rb, line 202</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">receive_response</span>&#x000A;  <span class="ruby-identifier">resp_length</span> = <span class="ruby-identifier">rpc_connection</span>.<span class="ruby-identifier">recv_varint</span>&#x000A;  <span class="ruby-identifier">resp_data</span> = <span class="ruby-identifier">rpc_connection</span>.<span class="ruby-identifier">recv</span> <span class="ruby-identifier">resp_length</span>&#x000A;  <span class="ruby-identifier">resp</span> = <span class="ruby-constant">PB</span><span class="ruby-operator">::</span><span class="ruby-constant">Response</span>.<span class="ruby-identifier">new</span>&#x000A;  <span class="ruby-identifier">resp</span>.<span class="ruby-identifier">parse_from_string</span> <span class="ruby-identifier">resp_data</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method protected-instance' id='method-method-i-send_request'>
                <a name='method-i-send_request'></a>
                <div class='synopsis'>
                  <span class='name'>send_request</span>
                  <span class='arguments'>(service, procedure, args, kwargs, param_names, param_types, param_default)</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-i-send_request-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-i-send_request-source'><span class="ruby-comment"># File lib/krpc/client.rb, line 197</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">send_request</span>(<span class="ruby-identifier">service</span>, <span class="ruby-identifier">procedure</span>, <span class="ruby-identifier">args</span>, <span class="ruby-identifier">kwargs</span>, <span class="ruby-identifier">param_names</span>, <span class="ruby-identifier">param_types</span>, <span class="ruby-identifier">param_default</span>)&#x000A;  <span class="ruby-identifier">req</span> = <span class="ruby-identifier">build_request</span>(<span class="ruby-identifier">service</span>, <span class="ruby-identifier">procedure</span>, <span class="ruby-identifier">args</span>, <span class="ruby-identifier">kwargs</span>, <span class="ruby-identifier">param_names</span>, <span class="ruby-identifier">param_types</span>, <span class="ruby-identifier">param_default</span>)&#x000A;  <span class="ruby-identifier">rpc_connection</span>.<span class="ruby-identifier">send</span> <span class="ruby-constant">Encoder</span>.<span class="ruby-identifier">encode_request</span>(<span class="ruby-identifier">req</span>)&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div id='footer-push'></div>
    </div>
    <div id='footer'>
      <a target="docwin" href="http://github.com/mislav/hanna/tree/master"><strong>Hanna</strong> RDoc template</a>
    </div>
  </body>
</html>
